const http = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalShow: true,
    year: 0,
    month: 0,
    date: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0
  },
  //获取日历相关参数
  dataTime: function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var months = date.getMonth() + 1;

    //获取现今年份
    this.data.year = year;

    //获取现今月份
    this.data.month = months;

    //获取今日日期
    this.data.getDate = date.getDate();

    //最后一天是几号
    var d = new Date(year, months, 0);
    this.data.lastDay = d.getDate();

    //第一天星期几
    let firstDay = new Date(year, month, 1);
    this.data.firstDay = firstDay.getDay();
  },

  //补签
  signature(){
    var uid = app.globalData.uid;
    var isToday = this.data.isToday;
    var dateArr = this.data.dateArr;
    var noSignArr = dateArr.filter(item => item.is_qian == 0 && item.dateNum > 0 && item.isToday<=isToday) || [];
    console.log(dateArr,noSignArr)
    if (noSignArr.length==0){
      $Toast({
        content: '暂无需要补签的日期',
        type: 'warning'
      });
     
      return
    }
    var bl_date = noSignArr[noSignArr.length - 1].isToday;
    if (bl_date == isToday) {
      $Toast({
        content: '请在签到页面签到!',
        type: 'warning'
      });
     
      return
    }
    wx.showModal({
      title: '提示',
      content: bl_date+'是否发起补签助力',
      success(res) {
        if (res.confirm) {
          http.fetchApi('procedure_t', { p: `R2015003|${uid}|${bl_date}|0|0` }).then(res => {
            if (res.data.code == 1) {
              if (res.data.results[0].codes == 1){
                $Toast({
                  content: '成功发起助力',
                  type: 'success'
                });
                setTimeout(() => {
                  wx.navigateTo({
                    url: '/pages/assistanceList/assistanceList',
                  })
                }, 1000)
              } else if (res.data.results[0].codes == 2){            
                $Toast({
                  content: '您这天已经发起过啦',
                  type: 'warning'
                });
                return
              }
             
            } else {      
              $Toast({
                content: '发起助力失败！',
                type: 'error'
              });
            }
          })
        } else if (res.cancel) {
         
        }
      }
    })
  },

  //日期补签
  lookHuoDong(e){
    var isToday = this.data.isToday;
    var is_qian = e.currentTarget.dataset.qian
    var uid = app.globalData.uid;
    var bl_date = e.currentTarget.dataset.date;
  //  var y = bl_date.slice(0,4);
  //   var m = bl_date.slice(4, 6);
  //   var d= bl_date.slice(6);
    // console.log(y,m,d)
    // bl_date=`${y}-${m}-${d}`
    if (bl_date>isToday){
      
      $Toast({
        content: '不可以补签还没到的日期',
        type: 'error'
      });
      return
    }

    if (is_qian == 1) {
      $Toast({
        content: '您这天已经签过到了',
        type: 'warning'
      });
      return
    }

    if (bl_date == isToday) {
      $Toast({
        content: '请在签到页面签到',
        type: 'warning'
      });
      return
    }

  


    wx.showModal({
      title: '提示',
      content: bl_date+'是否发起补签助力',
      success(res) {
        if (res.confirm) {
          http.fetchApi('procedure_t', { p: `R2015003|${uid}|${bl_date}|0|0` }).then(res=>{
            if(res.data.code==1){
              if (res.data.results[0].codes == 1) {
                $Toast({
                  content: '成功发起助力',
                  type: 'success'
                });
               setTimeout(()=>{
                 wx.navigateTo({
                   url: '/pages/assistanceList/assistanceList',
                 })
               },1000)
              } else if (res.data.results[0].codes == 2) {
                $Toast({
                  content: '您这天已经发起过啦',
                  type: 'warning'
                });
                return
              }
            }else{
              $Toast({
                content: '服务器异常',
                type: 'error'
              });
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  //获取签到天数和需要助力的人数
  getDayNum(){
    var uid=app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2015010|${uid}` }).then(res=>{
      if (res.data.code==1){
        var dayNum = res.data.results[0];
        this.setData({
          dayNum
        })
      }
    })
  },

  //打开活动规则弹窗
  ruleShow() {
    console.log(22)
    this.setData({
      modalShow: false
    })
  },
  //关闭活动规则弹窗
  closeModal() {
    this.setData({
      modalShow: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDayNum();
    //创建补0函数
    function p(s) {
      return s < 10 ? '0' + s : s;
    }
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    this.dateInit();
    this.setData({
      year: year,
      month: month,
      isToday: '' + year + p(month) + p(now.getDate())
    })
  },
  dateInit: function (setYear, setMonth) {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let dateArr = [];                       //需要遍历的日历数组数据
    let arrLen = 0;                         //dateArr的数组长度
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    let nextYear = 0;
    let month = setMonth || now.getMonth();                 //没有+1方便后面计算当月总天数
    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
    let startWeek = new Date((year + ',' + (month + 1) + ',' + 1).replace(/,/g, "/")).getDay(); 
   // console.log('startWeek', year + ',' + (month + 1) + ',' + 1, new Date(year + ',' + (month + 1) + ',' + 1))                         //目标月1号对应的星期
    let dayNums = new Date(year, nextMonth, 0).getDate();               //获取目标月有多少天
    let obj = {};
    let num = 0;
    //console.log('startWeek', startWeek, 'dayNums', dayNums)
    //创建补0函数
    function p(s) {
      return s < 10 ? '0' + s : s;
    }


    if (month + 1 > 11) {
      nextYear = year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek/1 + dayNums/1;
   // console.log('dateInit方法循环上面的一行', '长度为' + arrLen)
    for (let i = 0; i < arrLen; i++) {
     // console.log('进入循环了')
      if (i >= startWeek) {
        num = i - startWeek + 1;
        obj = {
          isToday: '' + year + p((month + 1)) + p(num),
          dateNum: num,
          weight: 5
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
      //console.log('每次循环的值', dateArr[i])
    }
    //console.log('循环结束后的一行')
    dateArr.forEach(item=>{item.is_qian=0})
    console.log('自己计算的',dateArr)
    var uid=app.globalData.uid,
      p = JSON.stringify(dateArr)
    http.fetchApi('p_date_qd', { p, uid }).then(res=>{
      if(res.data.code==1){
        var dateArr=res.data.data;
        console.log('后台返回的', dateArr)
        this.setData({
          dateArr: dateArr
        })
      }
    })
   
    let nowDate = new Date();
    let nowYear = nowDate.getFullYear();
    let nowMonth = nowDate.getMonth() + 1;
    let nowWeek = nowDate.getDay();
    let getYear = setYear || nowYear;
    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
    if (nowYear == getYear && nowMonth == getMonth) {
      this.setData({
        isTodayWeek: true,
        todayIndex: nowWeek
      })
    } else {
      this.setData({
        isTodayWeek: false,
        todayIndex: -1
      })
    }
  },
  /**
   * 上月切换
   */
  lastMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
    let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  /**
   * 下月切换
   */
  nextMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
    let month = this.data.month > 11 ? 0 : this.data.month;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})